import org.junit.Test;
import java.util.Arrays;
import java.util.List;


public class ArrayLearn {

    @Test
    public  void 排序() {
        // 随机生成Array
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 100);
        }
        System.out.println("随机生成的数组：");
        System.out.println(Arrays.toString(arr));


        //Array.sort(arr); =>作用是对数组进行排序，但是排序后数组的元素顺序会改变
        Arrays.sort(arr);//默认是升序排序，如果要降序排序，可以传入一个Comparator对象
        System.out.println("排序后的数组：");
        System.out.println("升序:" + Arrays.toString(arr));
        System.out.println("降序:" + Arrays.toString(Arrays.stream(arr).sorted().toArray()));

        //对对象数组进行排序
        String[] words = {"apple", "orange", "banana", "grape"};
        System.out.println("原始对象数组：" + Arrays.toString(words));
        Arrays.sort(words);
        System.out.println("排序后的对象数组：" + Arrays.toString(words));
    }

    @Test
    public  void 数组查询() {
        System.out.println("______________________________数组查询__");
        int[] arr = {1, 3, 5, 7, 9};
        int index = Arrays.binarySearch(arr, 5);
        String[] words = {"apple", "orange", "banana", "grape"};
        int index2 = Arrays.binarySearch(words, "orange");//找不到返回-1
        System.out.println(index); // 2
        System.out.println(index2);
    }

    @Test
    public  void 数组拷贝() {
        System.out.println("______________________________数组拷贝_");
        int[] arr = {1, 3, 5, 7, 9};
        // 完整复制
        int[] arr2 = Arrays.copyOf(arr, arr.length);
        System.out.println("完整复制："+Arrays.toString(arr2));
        System.out.println("部分"+Arrays.toString(Arrays.copyOfRange(arr, 1, 3)));
        System.out.println("从头复制"+Arrays.toString(Arrays.copyOf(arr,3)));
    }

    /**
     * Array转List
     * Arrays
     * */
    @Test
     public void Array转List(){
        System.out.println("_Array转List____________________________");
        String[] arr = {"apple", "banana", "orange"};
        List<String> list = Arrays.asList(arr);
        System.out.println(list); // [apple, banana, orange]
    }

    //fill(T[] a, T val)：将数组中的所有元素都设置为指定值。
    @Test
    public void FillArray(){
        System.out.println("_FillArray______________________________");
        int[] arr = new int[5];
        Arrays.fill(arr, 10);
        System.out.println(Arrays.toString(arr)); // [10, 10, 10, 10, 10]
    }

}
